<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/1/4
 * Time: 9:25
 */
namespace app\api\controller;
use think\Db;
class Index extends Base{
    public function index(){
        //查询名家
        $info = Db::table('up_person')->where('is_index','=',1)->find();

        //查询视频
        $info1 = Db::table('up_video')->alias('a')
            ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
            ->field('a.*,uvm.video_modular_name')
            ->where('is_type',1)
            ->where('a.is_index',1)->limit(2)->select();
        //查询音频
        $info2 = Db::table('up_music')->alias('a')
            ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
            ->field('a.*,umm.video_modular_name')
            ->where('a.is_index','=',1)->limit(3)->select();
        $res['person'] = $info;
        $res['video'] = $info1;
        $res['music'] = $info2;
        return json(['status' => 200, 'msg' => $res]);
    }

    //banner图
    public function banner(){
       $res =  Db::table('up_banner')->where('banner_part',1)->select();
       return json(['status' => 200, 'msg' => $res]);
    }

    //搜索
    public function keyword(){
        extract(input());
        $res = array();
      $type = input('param.type')?input('param.type'):1;
      $keyword = input('param.keyword');
      $page = input('param.page')?input('param.page'):1;
      $num = 10;
      $limit = $page-1;
      if($type==1){//视频
          $where['video_name'] = array('like',"%$keyword%");
          $where['is_type'] = 1;

          $res = Db::table('up_video')->alias('a')
              ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
              ->where($where)->limit($limit*$num,$num)
              ->field('a.*,uvm.video_modular_name')
              ->select();
          //查询是否收藏
          foreach ($res as $key=>&$vo){
              $where1['user_id'] = $user_id;
              $where1['is_type'] = 1;
              $where1['f_id'] = $vo['video_id'];
              $info = Db::table('up_like')->where($where1)->find();
              if($info['like_id']!=''){
                  $vo['is_like'] = 1;
              }else{
                  $vo['is_like'] = 2;
              }
          }


      }else if($type==2){//音频
          $where['music_name'] = array('like',"%$keyword%");
          $where['is_type'] = 1;
          $res = Db::table('up_music')->alias('a')
              ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
              ->where($where)->limit($limit*$num,$num)
              ->field('a.*,umm.video_modular_name')
              ->select();
          //查询是否收藏
          foreach ($res as $key=>&$vo){
              $where1['user_id'] = $user_id;
              $where1['is_type'] = 2;
              $where1['f_id'] = $vo['music_id'];
              $info = Db::table('up_like')->where($where1)->find();
              if($info['like_id']!=''){
                  $vo['is_like'] = 1;
              }else{
                  $vo['is_like'] = 2;
              }
          }
      }
        return json(['status' => 200, 'msg' => $res]);
    }

    //猜你喜欢
    public function like(){
        extract(input());
        //todo
        $where['user_id'] = $user_id;
        $num = Db::table('up_browse')->where($where)->count();
        if($num>0){
            //查询模块分类
            $browse_f_id = Db::table('up_browse')->where($where)->column('browse_f_id');
            $where1['video_f_id'] = array('IN',$browse_f_id);
            $res = Db::table('up_video')->alias('a')
                ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
                ->field('a.*,uvm.video_modular_name')
                ->where('is_type',1)
                ->where($where1)->orderRaw('rand()')->limit(3)->select();
        }else{
            $res = Db::table('up_video')->alias('a')
                ->join('up_video_modular uvm','uvm.video_modular_id = a.video_f_id','left')
                ->field('a.*,uvm.video_modular_name')
                ->where('is_type',1)
                ->orderRaw('rand()')->limit(3)->select();
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    //猜你喜欢浏览记录
    public function browse(){
        extract(input());
        //todo
        $f_id = input('param.browse_f_id');
        $where['user_id']=$user_id;
        $where['browse_f_id']=$f_id;
        $res = Db::table('up_browse')->where($where)->find();
        if($res['browse_id']==''){
            Db::table('up_browse')->insertGetId($where);
        }
        return json(['status' => 200, 'msg' => "成功"]);
    }
    //热门搜索
    public function hot(){
        $res =  Db::table('up_hot')->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //评论
    public function comment(){
        extract(input());
        //todo
        $comment = input('param.content');
        $type = input('param.type');
        $id = input('param.id');
        $data['user_id'] = $user_id;
        $data['content'] = $comment;
        $data['type'] = $type;
        $data['f_id'] = $id;
        $data['time'] = date('Y-m-d H:i:s',time());
        Db::table('up_comment')->insertGetId($data);
       return json(['status' => 200, 'msg' => "成功"]);
    }

    public function personDetail(){
        $id = input('param.id');
       $person_img = Db::table('up_person')->where('person_id',$id)->value('person_img');
        return json(['status' => 200, 'msg' => $person_img]);
    }

    //会员充值
    public function recharge(){
        $res = Db::table('up_recharge')
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    /***
     * @return \think\response\Json
     * 退出登录
     */
    public function  outTime(){
        extract(input());
        $res = Db::table('up_user_login_time')->where('user_id',$user_id)->order('user_login_id desc')->find();
        $dataList1['user_login_ent_time'] =date('Y-m-d H:i:s',time());
        Db::table('up_user_login_time')
            ->where('user_login_id', $res['user_login_id'])
            ->update($dataList1);
        return json(['status' => 200, 'msg' => $res]);
    }


}